package com.i2bt.app.files.model;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.files.FilesMoveActivity;
import com.i2bt.bean.FileBean;
import com.i2bt.bean.FileInfoBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class FilesMoveJsonHandle extends BaseJsonModel {

	public FilesMoveJsonHandle(BaseActivity activity) {
		super(activity);
	}

	@Override
	protected String logicToWrap() throws JSONException {
		
		switch (beanParam.getAct()) {

		// 获取资源目录
		case AndroidRestContract.GET_FILES : {
			jsonStrParam = wrapGetFiles();
			break;
		}
		
		// 文件移动
		case AndroidRestContract.MOVE_FILES : {
			jsonStrParam = wrapMoveFiles();
		}

		}
		
		return jsonStrParam;
	}
	
	/**
	 * 把获取目录的bean打包成json
	 * @return 打包后的json
	 * @throws JSONException
	 */
	private String wrapGetFiles() throws JSONException {
		
		wrapCommonJson();
		
		String parentFolder = "";
		try {
			parentFolder = URLEncoder.encode(
					((FileBean)beanParam).getParentFolder(), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		jsonParam.put(AndroidRestContract.PARENT_FOLODER, parentFolder);
		
		return jsonParam.toString();
	}
	
	/**
	 * 打包移动文件json
	 * @return
	 * @throws JSONException
	 */
	private String wrapMoveFiles() throws JSONException {
		
		wrapCommonJson();
		
		FileBean fileBean = (FileBean) beanParam;
		String[] previousPaths = fileBean.getPreviousPaths();
		// 对旧路径做中文编码, 并转换成json数组放入json对象中
		for (int i = 0; i < previousPaths.length; i++) {
			try {
				previousPaths[i] = URLEncoder.encode(
						previousPaths[i], "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		jsonParam.put(AndroidRestContract.PREVIOUS_PATH, 
				strsToJsonArray(previousPaths));
		
		// 对新路径做中文编码, 并放入
		String newPath = fileBean.getNewPath();
		try {
			jsonParam.put(AndroidRestContract.NEW_PATH,
					URLEncoder.encode(newPath, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return jsonParam.toString();
		
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		
		if (isSuc) {
			
			jsonParam = new JSONObject(result);

			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				FilesMoveActivity filesMoveActivity = (FilesMoveActivity)activity;

				switch (jsonParam.getInt(AndroidRestContract.ACTION)) {

				//获取资源
				case AndroidRestContract.GET_FILES : {
					filesMoveActivity.getFilesResponse(unwrapGetFiles());
					break;
				}
				
				// 移动文件
				case AndroidRestContract.MOVE_FILES : {
					filesMoveActivity.moveFilesResponse();
					break;
				}

				}
			}
			else {
				activity.toastHandler(jsonParam.
						getString(AndroidRestContract.MESSAGE));
			}
		}
		else {
			activity.toastHandler(result);
		}
	}
	
	
	/**
	 * 解包获取我的目录
	 * @throws JSONException
	 */
	private List<FileInfoBean> unwrapGetFiles() throws JSONException {
		System.out.println(jsonParam.toString());
		
		List<FileInfoBean> fileInfoList = new ArrayList<FileInfoBean>();
		JSONArray jsonArrayTemp = jsonParam.getJSONArray(
				((FileBean)beanParam).getParentFolder());
		for (int i = 0; i < jsonArrayTemp.length(); i++) {
			// 把文件夹放入列表中, 不存入文件
			if (jsonArrayTemp.getJSONArray(i).getLong(1) == 0) {
				fileInfoList.add( wrapFileBean(jsonArrayTemp.getJSONArray(i)) );
			}
		}
		
		return fileInfoList;
		
	}
	
	/**
	 * 把我的目录每一个数组都解包成bean
	 * @param jsonArray 我的目录json数组
	 * @return
	 * @throws JSONException
	 */
	private FileInfoBean wrapFileBean(JSONArray jsonArray) throws JSONException {
		FileInfoBean fileInfoBean = new FileInfoBean();
		fileInfoBean.setFileName(jsonArray.getString(0));
		long fileLength = jsonArray.getLong(1);
		if (fileLength != 0) {
			fileInfoBean.setFile(true);
		}
		fileInfoBean.setFileLength(fileLength);
		return fileInfoBean;
	}
	

}
